<?php

include_once("Model/thonxom_model.php");

class ThonXom extends MyController
{    
    public $model = null;
    
    public function __construct()
    {
        $this->model = new ThonXom_Model();
    }
    
    public function addView($note)
    {
        $rows = $this->model->getAllPhuongXa();
        $notice = $note;
        include("View/header.php");
        include("View/thonxom/add.php");
        include("View/footer.php");
    }
    
    public function getAllView($note)
    {
        $notice = $note;
        $rows = $this->model->getAllThonXom();
        include("View/header.php");
        include("View/thonxom/list.php");
        include("View/footer.php");
    }
    
    public function editView($maThonXom, $note)
    {
        $notice = $note;
        $row = mysql_fetch_array($this->model->getThonXom($maThonXom));
        $rows = $this->model->getAllPhuongXa();
        include("View/header.php");
        include("View/thonxom/edit.php");
        include("View/footer.php");
    }
    
    public function getAll()
    {
        $this->getAllView("");        
    }    
    
    public function add()
    {
        if(isset($_POST['submit']))
        {
            $maThonXom = $_POST['mathonxom'];
            $tenThonXom = $_POST['tenthonxom'];
            $maPhuongXa = $_POST['maphuongxa'];
            $task = $_POST['task'];
            
            if($this->model->checkExist($task, $maThonXom, $tenThonXom))
            {                
                $notice = Message::$addFail;
                $this->addView($notice);
                
            }
            else
            {
                $this->model->addThonXom($maThonXom, $tenThonXom, $maPhuongXa);
                $notice = Message::$addSuccess;                
                $this->addView($notice);                
            }
        }
        else
        {
            $notice = "";
            $this->addView($notice);
        }
    }
    
    public function edit()
    {
        if(isset($_GET['mathonxom']))
        {            
            $maThonXom = $_GET['mathonxom'];
            if(isset($_POST['submit']))
            {
                $tenThonXom = $_POST['tenthonxom'];
                $maPhuongXa = $_POST['maphuongxa'];
                $task = $_POST['task'];
                $result = mysql_fetch_array($this->model->getThonXom($maThonXom));
                if($tenThonXom == $result['tenthonxom'] && $maPhuongXa == $result['maphuongxa'])
                {
                    $notice = Message::$youDoNothing;
                    $this->getAllView($notice);
                }
                else
                {
                    if($tenThonXom == $result['tenthonxom'] && $maPhuongXa != $result['maphuongxa']) 
                    {
                        $this->model->editThonXom($maThonXom, $tenThonXom, $maPhuongXa);
                        $notice = Message::$editSuccess;
                        $this->getAllView($notice);
                    }
                    else
                    {
                        if($this->model->checkExist($task, $maThonXom, $tenThonXom))
                        {
                            $notice = Message::$editFail;
                            $this->editView($maThonXom, $notice);
                        }
                        else
                        {
                            $this->model->editThonXom($maThonXom, $tenThonXom, $maPhuongXa);
                            $notice = Message::$editSuccess;
                            $this->getAllView($notice);
                        }
                    }
                }
            }
            else
            {
                $this->editView($maThonXom, "");
            }
        }
        else
        {
            $this->getAllView("");
        }
    }
    
    public function delete()
    {
        if(isset($_GET['mathonxom']))
        {            
            $this->model->deleteThonXom($_GET['mathonxom']);
            $notice = Message::$deleteSuccess;
            $this->getAllView($notice);
        }
        else
        {
            $this->getAllView("");
        }
    }
}
